home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1992 June: ROMin Holiday / ADC Developer CD (1992-06) (''ROMin Holiday'')_iso / Developer Connection - 06-1992.iso / Developer Essentials / DTS Sample Code / Macintosh Sample Code / SC.010.EditTextCdev / EditCDEV.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-08-16  |  5.6 KB  |  198 lines  |  [TEXT/MPS ]

  1. /*------------------------------------------------------------------------------
  2. #
  3. #    Macintosh Developer Technical Support
  4. #
  5. #    EditText Sample Control Panel Device
  6. #
  7. #    EditCdev
  8. #
  9. #    EditCdev.c    -    C Source
  10. #
  11. #    Copyright © 1988 Apple Computer, Inc.
  12. #    All rights reserved.
  13. #
  14. #    Versions:    1.0                    8/88
  15. #
  16. #    Components:    EditCdev.p            August 1, 1988
  17. #                EditCdev.c            August 1, 1988
  18. #                EditCdev.r            August 1, 1988
  19. #                PEditCdev.make        August 1, 1988
  20. #                CEditCdev.make        August 1, 1988
  21. #
  22. #    EditCdev is a sample Control Panel device (cdev) that 
  23. #    demonstrates the usage of the edit-related messages.  
  24. #    EditCdev demonstrates how to implement an editText item
  25. #    in a Control Panel Device.  It utilizes the new undo, cut, copy,
  26. #    paste, and delete messages that are sent to cdevs in
  27. #    response to user menu selections.
  28. #
  29. #    It is comprised of two editText items that can be edited 
  30. #    and moved between via the mouse or tab key.
  31. #
  32. ------------------------------------------------------------------------------*/
  33.  
  34. #include <Types.h>
  35. #include <Memory.h>
  36. #include <Quickdraw.h>
  37. #include <TextEdit.h>
  38. #include <Dialogs.h>
  39. #include <Events.h>
  40. #include <Devices.h>
  41. #include <Scrap.h>
  42.  
  43. /* Constants */
  44. #define    textItm        1            /* first editTExt item in cdev */
  45.  
  46. #define    undoDev        9            /* cdev edit messages */
  47. #define    cutDev        10
  48. #define    copyDev        11
  49. #define    pasteDev    12
  50. #define    clearDev    13
  51.  
  52.  
  53. /* Types */
  54. typedef struct CDEVRec {
  55.     TEHandle    myTE;
  56. } CDEVRec, *CDEVPtr, **CDEVHnd;
  57.     
  58. /* Prototypes */
  59.  
  60. #ifndef MPW3
  61. void DoEditCommand ( /* short message, DialogPtr CPDialog */ );
  62. pascal Handle
  63. TextCDEV( /* short message, short item, short numItems, short CPanelID,
  64.          EventRecord *theEvent, Handle cdevStorage, DialogPtr CPDialog */ );
  65. #else
  66. void DoEditCommand (short message, DialogPtr CPDialog);
  67. pascal Handle
  68. TextCDEV(short message, short item, short numItems, short CPanelID,
  69.          EventRecord *theEvent, Handle cdevStorage, DialogPtr CPDialog);
  70. #endif
  71.  
  72. /* This is the main dispatcher. It must be the first code in the cdev.
  73.  EditCdev's dispatcher responds only to the following messages from
  74.  the Control Panel:
  75.      
  76.     macDev        - To indicate what machines it is available on.
  77.     initDev        - To set up some temporary storage and get the caret started.
  78.     keyEvtDev    - To check for an edit command and do the appropriate action.
  79.     cutDev        - To cut the current selection.
  80.     copyDev        - To copy the current selection.
  81.     pasteDev    - To paste the contents of the clipboard.
  82.     clearDev    - To delete the current selection.
  83.  
  84.  The Dialog Manager's services are used to handle entry of text, selection
  85.  of text, editing of text, and moving between the editText items via the
  86.  tab key. Since the Dialog Manager handles the selection of text, we do not
  87.  have to be concerned with hitDev messages for the editText items. The only
  88.  things we have to take care of are calling the Dialog Manager editing
  89.  routines in response to an edit command, and getting the caret to show up
  90.  at the beginning. In response to an edit command that was the result of
  91.  a command-key equivalent, we must also eliminate the event so that it does
  92.  not get processed as a keyDown by the Dialog Manager. Otherwise, an 'x'
  93.  would show up in the editText item when the user did a command-x to cut
  94.  the text.*/
  95.  
  96. pascal Handle
  97. TextCDEV(message,item,numItems,CPanelID,theEvent,cdevStorage,CPDialog)
  98.     short        message;
  99.     short        item;
  100.     short        numItems;
  101.     short        CPanelID;
  102.     EventRecord    *theEvent;
  103.     Handle        cdevStorage;
  104.     DialogPtr    CPDialog;
  105. {
  106. #ifdef MPW3
  107.     #pragma unused (item, CPanelID)        /* unused formal parameters */
  108. #endif
  109.     
  110.     char        tempChar;
  111.  
  112.     if (message == macDev) return((Handle) 1);                /* we work on every machine */
  113.     else if (cdevStorage != nil) {
  114.         switch (message) {
  115.             case initDev:                                    /* initialize cdev */
  116.                 cdevStorage = NewHandle(sizeof(CDEVRec));    /* create provate storage */
  117.                 SelIText(CPDialog, numItems + textItm, 0, 999); /* make caret show up */
  118.                 break;
  119.                 
  120.             case hitDev:                                    /* handle hit on item */
  121.             case closeDev:                                    /* clean up and dispose */
  122.             case nulDev:
  123.             case updateDev:                                    /* handle any update drawing */
  124.             case activDev:                                    /* activate any needed items */
  125.             case deactivDev:                                /* deactivate any needed items */
  126.                 break;
  127.                 
  128.             case keyEvtDev:                                    /* respond to keydown */
  129.                 tempChar = theEvent->message & charCodeMask;/* get the character, and check */
  130.                 if (theEvent->modifiers & cmdKey) {            /*  status of command key */
  131.                     message = nulDev;                        /* start with no message */
  132.                     theEvent->what = nullEvent;                /* and empty event type */
  133.                     
  134.                     switch (tempChar) {                        /* set appropriate message */
  135.                         
  136.                         case 'X':
  137.                         case 'x':
  138.                             message = cutDev;
  139.                             break;
  140.                         case 'C':
  141.                         case 'c':
  142.                             message = copyDev;
  143.                             break;
  144.                         case 'V':
  145.                         case 'v':
  146.                             message = pasteDev;
  147.                             break;
  148.                     }
  149.                     DoEditCommand(message, CPDialog);        /* Let edit command handler take it */
  150.                 }
  151.                 break;
  152.                 
  153.             case macDev:
  154.             case undoDev:
  155.                 break;
  156.                 
  157.             case cutDev:
  158.             case copyDev:
  159.             case pasteDev:
  160.             case clearDev:
  161.                 DoEditCommand(message, CPDialog);        /* respond to edit command */
  162.                 break;
  163.         }
  164.  
  165.         return (cdevStorage);
  166.     }  /* cdevStorage != nil */
  167.     
  168.     /* 
  169.     **    if cdevStorage = NIL then ControlPanel 
  170.     **  will put up memory error
  171.     */
  172.     return (nil);
  173. }
  174.  
  175. /* Call the appropriate Dialog Manager routine to handle an edit command for
  176.      an editText item. It will do all the work regarding the TEScrap. */
  177. void 
  178. DoEditCommand (message,CPDialog)
  179.     short        message;
  180.     DialogPtr    CPDialog;
  181. {
  182.     switch (message) {
  183.     case cutDev:
  184.         DlgCut(CPDialog);
  185.         break;
  186.     case copyDev:
  187.         DlgCopy(CPDialog);
  188.         break;
  189.     case pasteDev:
  190.         DlgPaste(CPDialog);
  191.         break;
  192.     case clearDev:
  193.         DlgDelete(CPDialog);
  194.         break;
  195.     }
  196. }
  197.  
  198.